//
//  HomeViewController.m
//  DesignPattern
//
//  Created by ClassWizard on 16/3/9.
//  Copyright © 2016年 ClassWizard. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeModel.h"

@interface HomeViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray<HomeModel *> *modelArr;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - UITableViewDelegate And DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeModel *model = self.modelArr[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = model.title;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeModel *model = self.modelArr[indexPath.row];
    if (model.isUseStoryboard) {
        [self performSegueWithIdentifier:model.className sender:nil];
    }
    else {
        UIViewController *viewController = [[NSClassFromString(model.className) alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

#pragma mark - lazy loading
- (NSMutableArray<HomeModel *> *)modelArr {
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
        [_modelArr addObject:[[HomeModel alloc] initWithTitle:@"工厂模式" className:@"FactoryMethodViewController" useStoryboard:YES]];
    }
    return _modelArr;
}

@end
